package org.csu.jpetstore.service;

import org.csu.jpetstore.domain.Category;
import org.csu.jpetstore.domain.Item;
import org.csu.jpetstore.domain.Product;
import org.csu.jpetstore.persistence.CategoryMapper;
import org.csu.jpetstore.persistence.ItemMapper;
import org.csu.jpetstore.persistence.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
import java.util.Map;

@Service
public class CatalogService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ItemMapper itemMapper;

    public Category getCategory(String categoryId) {
        return categoryMapper.getCategory(categoryId);
    }

    public List<Category> getCategoryList() {
        return categoryMapper.getCategoryList();
    }
    public Product getProduct(String productId) {
        return productMapper.getProduct(productId);
    }

    public List<Product> getProductListByCategory(String categoryId) {
        return productMapper.getProductListByCategory(categoryId);
    }
    public List<Item> getItemListByProduct(String productId) {
        return itemMapper.getItemListByProduct(productId);
    }

    public Item getItem(String itemId) {
        return itemMapper.getItem(itemId);
    }

    // 添加总类
    @Transactional
    public void addCategory(Category category) {
        categoryMapper.insertCategory(category);
    }

    // 更新总类
    @Transactional
    public void updateCategory(Category category) {
        categoryMapper.updateCategory(category);
    }

    // 删除总类
    @Transactional
    public void deleteCategory(String categoryId) {
        categoryMapper.deleteCategory(categoryId);
        categoryMapper.deleteProductByCategory(categoryId);
        categoryMapper.deleteCategoryById(categoryId);
    }

    // 添加大类
    @Transactional
    public void addProduct(Product product) {
        productMapper.insertProduct(product);
    }

    // 更新大类
    @Transactional
    public void updateProduct(Product product) {
        productMapper.updateProduct(product);
    }

    // 删除大类
    @Transactional
    public void deleteProduct(String productId) {
        productMapper.deleteItemByProduct(productId);
        productMapper.deleteProduct(productId);
    }

    // 添加小类
    @Transactional
    public void addItem(Item item,int quantity) {
        itemMapper.insertItem(item);
        itemMapper.insertInventory(item.getItemId(),quantity);
    }

    // 更新小类
    @Transactional
    public void updateItem(Item item) {
        itemMapper.updateItem(item);
    }

    // 删除小类
    @Transactional
    public void deleteItem(String itemId) {
        itemMapper.deleteItem(itemId);
    }
}

